﻿
Partial Class WebForm_ManageVolunteer_AddPerson
    Inherits System.Web.UI.UserControl

    Protected Sub Page_Init(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Init
        Me.fv.InsertItemTemplate = Me.fv.ItemTemplate
        Me.fv.EditItemTemplate = Me.fv.ItemTemplate
    End Sub

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        'Me.Visible = False

        If Not IsPostBack Then

        End If
    End Sub

    Protected Sub fv_DataBound(ByVal sender As Object, ByVal e As System.EventArgs) Handles fv.DataBound
        Me.BindDDLActive()
        Me.BindDDLPOrganizationID()
        Me.BindDDLGender()
        Me.BindDDLEducationLevel()
        Me.BindDDLOccupationType()
        Me.BindDDLAdmin()
        If Me.fv.CurrentMode = FormViewMode.Insert Then
            Dim ddlActive As DropDownList = TryCast(Me.fv.FindControl("ddlActive"), DropDownList)

            ddlActive.SelectedIndex = ddlActive.Items.IndexOf(ddlActive.Items.FindByValue("Y"))

            Me.BindDataForInsert()
        ElseIf Me.fv.CurrentMode = FormViewMode.Edit Then

            If DataBinder.Eval(fv.DataItem, "Gender") Is DBNull.Value Then
                TryCast(Me.fv.FindControl("ddlGender"), DropDownList).SelectedValue = ""
            Else
                TryCast(Me.fv.FindControl("ddlGender"), DropDownList).SelectedValue = DataBinder.Eval(fv.DataItem, "Gender")
            End If

            If DataBinder.Eval(fv.DataItem, "OccupationType") Is DBNull.Value Then
                TryCast(Me.fv.FindControl("ddlOccupationType"), DropDownList).SelectedValue = ""
            Else
                TryCast(Me.fv.FindControl("ddlOccupationType"), DropDownList).SelectedValue = DataBinder.Eval(fv.DataItem, "OccupationType")
            End If

            If DataBinder.Eval(fv.DataItem, "EducationLevel") Is DBNull.Value Then
                TryCast(Me.fv.FindControl("ddlEducationLevel"), DropDownList).SelectedValue = ""
            Else
                TryCast(Me.fv.FindControl("ddlEducationLevel"), DropDownList).SelectedValue = DataBinder.Eval(fv.DataItem, "EducationLevel")
            End If

            Dim ddlActive As DropDownList = TryCast(Me.fv.FindControl("ddlActive"), DropDownList)

            ddlActive.SelectedValue = DataBinder.Eval(Me.fv.DataItem, "Active")

            Dim ddlAdmin As DropDownList = TryCast(Me.fv.FindControl("ddlAdmin"), DropDownList)

            If DataBinder.Eval(fv.DataItem, "IsAdmin") Is DBNull.Value Then
                ddlAdmin.SelectedValue = ""
            Else
                ddlAdmin.SelectedValue = DataBinder.Eval(Me.fv.DataItem, "IsAdmin")
            End If

            TryCast(Me.fv.FindControl("txtPersonID"), TextBox).Enabled = False
        End If
    End Sub

    Protected Sub fv_ItemCommand(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.FormViewCommandEventArgs) Handles fv.ItemCommand
        If e.CommandName = "btnSave" Then
            If Me.SaveData() = True Then
                RaiseEvent Saved(Me, New EventArgs)
            End If
        End If
    End Sub
End Class

Partial Class WebForm_ManageVolunteer_AddPerson
    Private Sub BindDDLAdmin()
        Dim ddlAdmin As DropDownList = TryCast(Me.fv.FindControl("ddlAdmin"), DropDownList)

        ddlAdmin.DataTextField = "Text"
        ddlAdmin.DataValueField = "Value"
        ddlAdmin.DataSource = MyObj.GetCodeMappingData("CleanPlan", "IsPublic")
        ddlAdmin.DataBind()

        ddlAdmin.Items.Insert(0, MyObj.GetDDLPleaseChooseItem())

    End Sub
    Private Sub BindDDLActive()
        Dim ddlActive As DropDownList = TryCast(Me.fv.FindControl("ddlActive"), DropDownList)

        ddlActive.DataTextField = "Text"
        ddlActive.DataValueField = "Value"
        ddlActive.DataSource = MyObj.GetCodeMappingData("Person", "Active")
        ddlActive.DataBind()


    End Sub

    Private Sub BindDDLGender()
        Dim ddlGender As DropDownList = TryCast(Me.fv.FindControl("ddlGender"), DropDownList)

        ddlGender.DataTextField = "Text"
        ddlGender.DataValueField = "Value"
        ddlGender.DataSource = MyObj.GetCodeMappingData("Employee", "Gender")
        ddlGender.DataBind()
        ddlGender.Items.Insert(0, MyObj.GetDDLPleaseChooseItem())


    End Sub

    Private Sub BindDDLOccupationType()
        Dim ddlOccupationType As DropDownList = TryCast(Me.fv.FindControl("ddlOccupationType"), DropDownList)

        ddlOccupationType.DataTextField = "Text"
        ddlOccupationType.DataValueField = "Value"
        ddlOccupationType.DataSource = MyObj.GetCodeMappingData("Employee", "OccupationType")
        ddlOccupationType.DataBind()
        ddlOccupationType.Items.Insert(0, MyObj.GetDDLPleaseChooseItem())

    End Sub

    Private Sub BindDDLEducationLevel()
        Dim ddlEducationLevel As DropDownList = TryCast(Me.fv.FindControl("ddlEducationLevel"), DropDownList)

        ddlEducationLevel.DataTextField = "Text"
        ddlEducationLevel.DataValueField = "Value"
        ddlEducationLevel.DataSource = MyObj.GetCodeMappingData("Employee", "EducationLevel")
        ddlEducationLevel.DataBind()
        ddlEducationLevel.Items.Insert(0, MyObj.GetDDLPleaseChooseItem())

    End Sub

    Private Sub BindDDLPOrganizationID()
        Dim ddlPOrganizationID As DropDownList = TryCast(Me.fv.FindControl("ddlPOrganizationID"), DropDownList)
        Dim dat As New clsData
        Dim sql As New MyStringBuilder

        sql.AppendFormat("select v.* ")
        sql.AppendFormat("from VOrganization v ")
        sql.AppendFormat("where v.OrganizationType = 'P' ")
        sql.AppendFormat("and v.Active='Y' ")
        ddlPOrganizationID.DataTextField = "VOrganizationName"
        ddlPOrganizationID.DataValueField = "VOrganizationID"
        ddlPOrganizationID.DataSource = dat.GetData(sql)
        ddlPOrganizationID.DataBind()
        ddlPOrganizationID.Items.Insert(0, MyObj.GetDDLPleaseChooseItem())

        ddlPOrganizationID.Text = DataBinder.Eval(Me.fv.DataItem, "VOrganizationID")

    End Sub

    ''' <summary>
    ''' 新增模式
    ''' </summary>
    ''' <remarks></remarks>
    Public Sub Show()

        Me.fv.ChangeMode(FormViewMode.Insert)
        Me.fv.DataBind()
        'Me.VOrganizationID = vorganizationID

        Me.BindDataForInsert()
        'Me.mpSearch.Show()
        Me.Visible = True
    End Sub

    ''' <summary>
    ''' 編輯模式
    ''' </summary>
    ''' <param name="personID"></param>
    ''' <remarks></remarks>
    Public Sub ShowEdit(ByVal personID As String)

        Me.fv.ChangeMode(FormViewMode.Edit)

        Me.BindDataForEdit(personID)
        'Me.mpSearch.Show()
        Me.Visible = True
    End Sub

    Private Sub BindDataForInsert()
        'Dim dat As New clsData
        'Dim sql As New MyStringBuilder

        'sql.AppendFormat("select o.* ")
        'sql.AppendFormat("from VOrganization o ")
        'sql.AppendFormat("where o.VOrganizationID = {0} ", Me.VOrganizationID)

        'Dim dt As Data.DataTable = dat.GetData(sql)

        'TryCast(Me.fv.FindControl("txtVOrganizationName"), Label).Text = dt.Rows(0)("VOrganizationName")

    End Sub

    Private Sub BindDataForEdit(ByVal personID As String)
        Dim dat As New clsData
        Dim sql As New MyStringBuilder

        sql.AppendFormat("select p.*, v.VOrganizationName ")
        sql.AppendFormat("from Person p ")
        sql.AppendFormat("inner join VOrganization v on p.VOrganizationID = v.VOrganizationID ")
        sql.AppendFormat("where p.PersonID = '{0}' ", personID)

        Dim dt As Data.DataTable = dat.GetData(sql)

        Me.fv.DataSource = dt
        Me.fv.DataBind()
    End Sub

    Private Function SaveData() As Boolean
        Dim dat As New clsData
        Dim sql As New MyStringBuilder

        Using scope As New Transactions.TransactionScope
            If Me.fv.CurrentMode = FormViewMode.Insert Then
                sql.Length = 0
                sql.AppendFormat("select UserID from [User] ")
                sql.AppendFormat("where UserID = '{0}' ", Me.PersonID)

                If dat.GetData(sql).Rows.Count > 0 Then
                    MyObj.ShowMessage("系統已存在此帳號")
                    Return False
                End If

                sql.Length = 0
                sql.AppendFormat("insert Person (PersonID,PersonName,Email,PhoneNumber,FaxPhoneNumber,VOrganizationID,Password,IdentificationID,Gender,OccupationType,EducationLevel,Birthday,Active,IsAdmin) select ")
                sql.AppendFormat(" '{0}' ", TryCast(Me.fv.FindControl("txtPersonID"), TextBox).Text)
                sql.AppendFormat(",'{0}' ", TryCast(Me.fv.FindControl("txtPersonName"), TextBox).Text)
                sql.AppendFormat(",'{0}' ", TryCast(Me.fv.FindControl("txtEmail"), TextBox).Text)
                sql.AppendFormat(",'{0}' ", TryCast(Me.fv.FindControl("txtPhoneNumber"), TextBox).Text)
                sql.AppendFormat(",'{0}' ", TryCast(Me.fv.FindControl("txtFaxPhoneNumber"), TextBox).Text)
                sql.AppendFormat(",{0} ", Me.VOrganizationID)
                sql.AppendFormat(",Password='{0}' ", clsAuth.EncryptPassword(TryCast(Me.fv.FindControl("txtPassword"), TextBox).Text))
                sql.AppendFormat(",'{0}' ", TryCast(Me.fv.FindControl("txtIdentificationID"), TextBox).Text)
                sql.AppendFormat(",'{0}' ", TryCast(Me.fv.FindControl("ddlGender"), DropDownList).SelectedValue)
                sql.AppendFormat(",'{0}' ", TryCast(Me.fv.FindControl("ddlOccupationType"), DropDownList).SelectedValue)
                sql.AppendFormat(",'{0}' ", TryCast(Me.fv.FindControl("ddlEducationLevel"), DropDownList).SelectedValue)
                sql.AppendFormat(",'{0:yyyy/MM/dd}' ", DateTime.Parse(TryCast(Me.fv.FindControl("txtBirthday"), WebForm_CommonControl_CalendarTextBox).Text))
                sql.AppendFormat(",'{0}' ", "Y")
                sql.AppendFormat(",'{0}'", TryCast(Me.fv.FindControl("ddlAdmin"), DropDownList).SelectedValue)
                dat.AccData(sql)
            Else
                sql.Length = 0
                sql.AppendFormat("update Person set ")
                sql.AppendFormat("PersonName='{0}' ", TryCast(Me.fv.FindControl("txtPersonName"), TextBox).Text)
                sql.AppendFormat(",VOrganizationID='{0}' ", TryCast(Me.fv.FindControl("ddlPOrganizationID"), DropDownList).Text)
                sql.AppendFormat(",Email='{0}' ", TryCast(Me.fv.FindControl("txtEmail"), TextBox).Text)
                sql.AppendFormat(",PhoneNumber='{0}' ", TryCast(Me.fv.FindControl("txtPhoneNumber"), TextBox).Text)
                sql.AppendFormat(",FaxPhoneNumber='{0}' ", TryCast(Me.fv.FindControl("txtFaxPhoneNumber"), TextBox).Text)

                If TryCast(Me.fv.FindControl("txtPassword"), TextBox).Text <> "" Then
                    sql.AppendFormat(",Password='{0}' ", clsAuth.EncryptPassword(TryCast(Me.fv.FindControl("txtPassword"), TextBox).Text))
                End If
                sql.AppendFormat(",IdentificationID='{0}' ", TryCast(Me.fv.FindControl("txtIdentificationID"), TextBox).Text)
                sql.AppendFormat(",Gender='{0}' ", TryCast(Me.fv.FindControl("ddlGender"), DropDownList).SelectedValue)
                sql.AppendFormat(",OccupationType='{0}' ", TryCast(Me.fv.FindControl("ddlOccupationType"), DropDownList).SelectedValue)
                sql.AppendFormat(",EducationLevel='{0}' ", TryCast(Me.fv.FindControl("ddlEducationLevel"), DropDownList).SelectedValue)
                sql.AppendFormat(",Birthday='{0:yyyy/MM/dd}' ", DateTime.Parse(TryCast(Me.fv.FindControl("txtBirthday"), WebForm_CommonControl_CalendarTextBox).Text))
                sql.AppendFormat(",Active='{0}' ", TryCast(Me.fv.FindControl("ddlActive"), DropDownList).SelectedValue)
                sql.AppendFormat(",IsAdmin='{0}' ", TryCast(Me.fv.FindControl("ddlAdmin"), DropDownList).SelectedValue)
                sql.AppendFormat("where PersonID='{0}' ", Me.PersonID)
                dat.AccData(sql)

            End If

            scope.Complete()
        End Using

        Return True
    End Function

    Public ReadOnly Property PersonID() As String
        Get
            Return TryCast(Me.fv.FindControl("txtPersonID"), TextBox).Text
        End Get
    End Property

    Public ReadOnly Property PersonName() As String
        Get
            Return TryCast(Me.fv.FindControl("txtPersonName"), TextBox).Text
        End Get
    End Property

    Private Property VOrganizationID() As String
        Get
            'Return ViewState("VOrganizationID")
            Return TryCast(Me.fv.FindControl("ddlPOrganizationID"), DropDownList).SelectedValue
        End Get
        Set(ByVal value As String)
            ViewState("VOrganizationID") = value
        End Set
    End Property

    Public Event Saved(ByVal sender As Object, ByVal e As System.EventArgs)
    'Public Event Updated(ByVal sender As Object, ByVal e As System.EventArgs)
End Class